﻿using System;
using System.Data;
using System.Data.SqlClient;
using prjKQXS_DLL;

namespace prjKQXS_Report.DMQUY
{
    public partial class rptQuy_Nam : DevExpress.XtraReports.UI.XtraReport
    {
        //Cài đặt event
        public delegate void OnLoadCompleteHandler();
        public event OnLoadCompleteHandler OnLoadComplete;

        dsReports dsReports1 = new dsReports();

        public rptQuy_Nam()
        {
            InitializeComponent();
        }

        private void rptdmquynam_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            GroupFooter1.Visible = pMaDV.Value.ToString().Trim() == "%";

            lblDonvi.Text = "Phòng XSKT " + pTenDV.Value.ToString().Trim();
            xrlblNam.Text = "Năm " + ((DateTime)pNgay.Value).Year.ToString();

            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);
            dsReports1.Tables.Remove("DMQUY1");
            dsReports1.Tables.Add(xuly());
            this.DataSource = dsReports1;
            this.DataMember = "DMQUY1";
        }

        private void rptdmquynam_AfterPrint(object sender, EventArgs e)
        {
            OnLoadComplete();
        }
        
        private DataTable xuly()
        {
            decimal tongsothu = 0;
            decimal tongsochi = 0;
            decimal tongso = 0;
            string sql = @"Select MONTH(Ngay) as Thang
                                , Sum(Sotienthu) AS Sotienthu
                                , Sum(Sotienchi) AS Sotienchi
                                , D.Ma_DT AS Madv
                                , D.Ten_DT AS TenDV
                            from DMQUY Q INNER JOIN DMDONVI D ON D.Ma_DT = Q.Madv
                            Where rtrim(Madv) like @MaDV
                                and YEAR(Ngay) = YEAR(@Ngay)
                            Group by MONTH(Ngay), D.Ma_DT, D.Ten_DT";

            SqlConnection conn = new SqlConnection(BienTc.connString);
            if (conn.State == ConnectionState.Closed) conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.SelectCommand.Parameters.AddWithValue("@Ngay", ((DateTime)pNgay.Value).Date);
            da.SelectCommand.Parameters.AddWithValue("@MaDV", pMaDV.Value.ToString().Trim());

            DataTable dt = new DataTable("DMQUY1");
            da.Fill(dt);

            dt.Columns.Add("TonQuy", typeof(decimal));

            if (dt.Rows.Count > 0)
            {
                decimal thu = 0, chi=0;
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Sotienthu"] != null && dr["Sotienthu"].ToString().Trim() != "" && (decimal)clsCommon.Parse(dr["Sotienthu"].ToString().Trim(), 3) != 0)
                    {
                        thu = (decimal)clsCommon.Parse(dr["Sotienthu"].ToString().Trim(), 3);
                        tongsothu += thu;
                    }
                    if (dr["Sotienchi"] != null && dr["Sotienchi"].ToString().Trim() != "" && (decimal)clsCommon.Parse(dr["Sotienchi"].ToString().Trim(), 3) != 0)
                    {
                        chi = (decimal)clsCommon.Parse(dr["Sotienchi"].ToString().Trim(), 3);
                        tongsochi += chi;
                    }

                    dr["TonQuy"] = thu - chi;
                    thu = 0;
                    chi = 0;
                }
            }

            tongso = tongsothu - tongsochi;
            //lbltonquy.Text = tongso.ToString();
            lbltonquy.Text = String.Format("{0:0,0.000}", double.Parse(tongso.ToString()));

            return dt;
        }

    }
}
